const SQLITE_NAME = "amins_mobile";
const SQLITE_PATH = `_doc/${SQLITE_NAME}.db`;

// 打开数据库
function openDB(successFun , failFun) {
	console.log('sqliteUtil openDB');
	plus.sqlite.openDatabase({
		name: SQLITE_NAME,
		path: SQLITE_PATH,
		success: function(e) {
			console.log('openDatabase success!');
			successFun(e);
		},
		fail: function(e) {
			console.log('openDatabase failed: ' + JSON.stringify(e));
			failFun(e);
		}
	});
}

//关闭数据库
function closeDB() {
	plus.sqlite.closeDatabase({
		name: SQLITE_NAME,
		success(e) {
			resolve(e);
		},
		fail(e) {
			reject(e);
		}
	})
}

//监听数据库是否开启
function isDbOpen() {
	let open = plus.sqlite.isOpenDatabase({
		name: SQLITE_NAME,
		path: SQLITE_PATH,
	})
	return open;
}

/**
 * 执行增删改等操作的SQL语句
 * @param {Object} inSql sql语句
 * @param {Object} successFun 成功回调
 * @param {Object} failFun 失败回调
 */
function executeSql(inSql , successFun, failFun) {
	plus.sqlite.executeSql({
		name: SQLITE_NAME,
		sql: inSql,
		success(e) {
			successFun(e);
		},
		fail(e) {
			failFun(e);
		}
	})
}

/**
 * 查询数据
 * @param {Object} inSql sql语句
 * @param {Object} successFun 成功回调
 * @param {Object} failFun 失败回调
 */
function selectSql(inSql , successFun, failFun) {
	console.log("selectSql  start ==== " + inSql )
	plus.sqlite.selectSql({
		name: SQLITE_NAME,
		sql: inSql,
		success(e) {
			console.log("selectSql  Success")
			successFun(e);
		},
		fail(e) {
			console.log("selectSql  fail")
			failFun(e);
		}
	})
}

export {
	SQLITE_NAME as SQLITE_NAME,
	openDB as openDB,
	closeDB as closeDB,
	isDbOpen as isDbOpen,
	executeSql as executeSql,
	selectSql as selectSql,
}
